package com.house.health.controller;

import com.house.health.domain.*;
import com.house.health.service.IAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author DuanChangSuo
 * @date 2023/4/14 10:38
 * @description 类的描述和介绍
 **/
@Controller
public class AnnouncementController {

    @Autowired
    private IAnnouncementService iAnnouncementService;

    /**
     * 管理员点击发布发布公告，跳转页面
     * @return
     */
    @RequestMapping("/addAnnouncement")
    public String addAnnouncement() {
        return "addannouncement";
    }

    /**
     * 点击现在发布,发布公告
     */
    @RequestMapping("/addAnnouncementRecord")
    @ResponseBody
    public String addAnnouncement(Announcement announcement) {
        if(announcement.getPublisher()==null||"".equals(announcement.getPublisher())) {
            announcement.setPublisher("管理员");
        }
        int n = iAnnouncementService.addNewAnnouncement(announcement);
        if(n>0) {
            return "OK";
        }
        return "FAIL";
    }

    /**
     * 管理员点击所有公告，跳转页面
     * @return
     */
    @RequestMapping("/toAllAnnouncementPage1")
    public String toAllAnnouncementPage1() {
        return "allannouncement1";
    }

    /**
     * 显示所有公告
     * @return
     */
    @RequestMapping("/findAllAnnouncement1")
    @ResponseBody
    public AnnouncementData findAllAnnouncement1() {
        List<Announcement> findAllAnnouncement = iAnnouncementService.findAllAnnouncement();
        AnnouncementData u = new AnnouncementData();
        u.setCode(0);
        u.setCount(findAllAnnouncement.size());
        u.setData(findAllAnnouncement);
        u.setMsg("OK");
        return u;
    }

    /**
     * 用户点击系统公告，跳转页面
     * @return
     */
    @RequestMapping("/toAllAnnouncementPage2")
    public String toAllAnnouncementPage2() {
        return "allannouncement2";
    }

    /**
     * 点击查看，跳转弹窗
     * @param aID
     * @return
     */
    @RequestMapping("/AnnouncementPage")
    @ResponseBody
    public String toAnnouncementPage(int aID) {
        Announcement announcement = iAnnouncementService.findAnnouncementDetailById(aID);
        return announcement.getAnnouncementDesc();
    }

    /**
     * 点击修改，跳转到更新界面
     * @param aID
     * @param request
     * @return
     */
    @RequestMapping("/toUpdateAnnouncementPage")
    public String toUpdateAnnouncementPage(int aID, HttpServletRequest request) {
        Announcement announcement = iAnnouncementService.findAnnouncementDetailById(aID);
        request.getSession().setAttribute("Announcement", announcement);
        return "updateannouncement";
    }

    /**
     * 管理员更新公告信息
     * @param announcement
     * @return
     */
    @RequestMapping("/updateAnnouncement")
    @ResponseBody
    public String updateAnnouncement(Announcement announcement) {
        int n = iAnnouncementService.updateAnnouncement(announcement);
        if(n>=0)
            return "OK";
        return "FAIL";
    }

    /**
     * 管理员删除公告
     * @param aID
     * @return
     */
    @RequestMapping("/deleteAnnouncement")
    @ResponseBody
    public String deleteAnnouncement(int aID) {
        int deleteAnnouncement = iAnnouncementService.deleteAnnouncement(aID);
        if (deleteAnnouncement > 0) {
            return "OK";
        }
        return "FAIL";
    }

    @RequestMapping("/toChatPage")
    public String aaa() {
        return "1";
    }
}
